<template>
  <view class="my-settle-container">
    <!-- 全选区域 -->
    <label class="radio">
      <radio color="#C00000" :checked="isCheckAll" @click="updateState" /><text>全选</text>
    </label>

    <!-- 合计区域 -->
    <view class="amount-box">
      合计:<text class="amount">￥{{checkGoodsPrice}}</text>
    </view>

    <!-- 结算按钮 -->
    <view class="btn-settle" @click="settlement">结算({{checkoutAmount}})</view>
  </view>
</template>
<script>
  import {
    mapGetters,
    mapMutations,
    mapState
  } from 'vuex'
  export default {
    data() {
      return {
        seconds: 4,
        timer: null
      }
    },
    computed: {
      ...mapGetters('m_cart', ['checkoutAmount', 'total', 'checkGoodsPrice']),
      ...mapGetters('m_user', ['adressFull']),
      ...mapState('m_user', ['token']),
      ...mapState('m_cart',['cartList']),
      isCheckAll() {
        return this.checkoutAmount == this.total
      }
    },
    methods: {
      ...mapMutations('m_cart', ['checkAllGoodsState']),
      ...mapMutations('m_user', ['updateRedirect']),
      updateState() {
        this.checkAllGoodsState(!this.isCheckAll)
      },
      settlement() {
        if (this.checkoutAmount <= 0) return uni.$showMsg('请选择商品')
        if (!this.adressFull) return uni.$showMsg('请选择地址')
        // if(!this.token) return uni.$showMsg('请先登录')
        if (!this.token) {
          this.timer = setInterval(() => {
            this.seconds--
            if (this.seconds <= 0) {
              clearInterval(this.timer)
              uni.switchTab({
                url: '/pages/my/my',
                success : () => {
                  this.updateRedirect({
                    openType : 'switchTap',
                    form : '/pages/cart/cart'
                  })
                }
              })
              return this.seconds = 3
            }
            this.showTip()
          }, 1000)
        }
        this.goPay()
      },
      showTip() {
        uni.showToast({
          icon: 'none',
          title: '请登录后在结算，' + this.seconds + '秒后跳转到登录界面',
          mask: true
        })
      },
      async goPay() {
         const orderInfo = {
            // 开发期间，注释掉真实的订单价格，
            // order_price: this.checkedGoodsAmount,
            // 写死订单总价为 1 分钱
            order_price: 0.01,
            consignee_addr: this.adressFull,
            goods: this.cartList.filter(x => x.goods_state).map(x => ({ goods_id: x.goods_id, goods_number: x.goods_count, goods_price: x.goods_price }))
          }
          const {data} = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
          if(data.meta.status !== 200) return uni.showMsg('订单创建失败')
          const orderNumber = data.message.order_number
           const { data: res2 } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', { order_number: orderNumber })
            // 2.2 预付订单生成失败
            if (res2.meta.status !== 200) return uni.$showError('预付订单生成失败！')
            // 2.3 得到订单支付相关的必要参数
            const payInfo = res2.message.pay
      }
    }
  }
</script>
<style lang="scss">
  .my-settle-container {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 50px;
    // 将背景色从 cyan 改为 white
    background-color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 5px;
    font-size: 14px;

    .radio {
      display: flex;
      align-items: center;
    }

    .amount {
      color: #c00000;
    }

    .btn-settle {
      height: 50px;
      min-width: 100px;
      background-color: #c00000;
      color: white;
      line-height: 50px;
      text-align: center;
      padding: 0 10px;
    }
  }
</style>
